package com.boilerCloud.dao;

import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import com.boilerCloud.dto.HzlqswPage;
import com.boilerCloud.util.SQLUtil;

public class BaseDao {

	@Resource
	
	public JdbcTemplate jdbcTemplate;
	
	private final static String DEFAULT_SQL_TYPE = SQLUtil.DBMS_MYSQL_TYPE;
	
	private static final Logger log = LoggerFactory.getLogger(BaseDao.class);
	
	
	/**
	 * 查询单个对象
	 * @param sql sql语句
	 * @param clazz 
	 * @return
	 */
	public <T> T queryForObject(String sql,Class<T> clazz) throws Exception{
		try {
			if(clazz == Integer.class || clazz == String.class || clazz == Long.class){
				return jdbcTemplate.queryForObject(sql,clazz);
			}
			return jdbcTemplate.queryForObject(sql,new BeanPropertyRowMapper<T>(clazz));
		} catch (Exception e) {
			log.error("",e);
			return null;
		}
		
		
	}
	
	
	
	public <T> T queryForObject(String sql,Class<T> clazz,Object ... args)throws Exception{
		try {
			if(clazz == Integer.class || clazz == String.class || clazz == Long.class){
				return jdbcTemplate.queryForObject(sql,clazz,args);
			}
			return  jdbcTemplate.queryForObject(sql,new BeanPropertyRowMapper<T>(clazz),args);
		} catch (Exception e) {
			log.error("",e);
			return null;
		}

	}
	
	

	public <T> T queryForObject(String sql,Class<T> clazz,List<Object> params)throws Exception{
		
		try {
			if(clazz == Integer.class || clazz == String.class || clazz == Long.class){
				return jdbcTemplate.queryForObject(sql,clazz,params.toArray());
			}
			return  jdbcTemplate.queryForObject(sql,new BeanPropertyRowMapper<T>(clazz),params.toArray());
		} catch (Exception e) {
			log.error("",e);
			return null;
		}
	}
	
	public <T> List<T> queryForList(String sql,Class<T> clazz)throws Exception{
		if(clazz == Integer.class || clazz == String.class || clazz == Long.class){
			return jdbcTemplate.queryForList(sql,clazz);
		}
		
		return  jdbcTemplate.query(sql,new BeanPropertyRowMapper<T>(clazz));
		
	}
	
	
	public static <T>void  tt (List<T> list){
	
	}
	
	public  <T> Class<T> tt1() throws NoSuchMethodException, SecurityException{
		Method methodApply = this.getClass().getDeclaredMethod("tt", List.class);
		ParameterizedType pType = (ParameterizedType)(methodApply .getGenericParameterTypes())[0];
        System.out.println("setDates("
                   + ((Class) pType.getRawType()).getName() + "<"
                   + ((Class) (pType.getActualTypeArguments()[0])).getName()
                   + ">)" );


		return (Class) (pType.getActualTypeArguments()[0]);
		
	}
	public <T> List<T> queryForList(String sql,Class<T> clazz,Object ... args)throws Exception{
		if(clazz == Integer.class || clazz == String.class || clazz == Long.class){
			return jdbcTemplate.queryForList(sql,clazz,args);
		}
	
		return  jdbcTemplate.query(sql,new BeanPropertyRowMapper<T>(clazz),args);
		
	}
	
	public <T> List<T> queryForList( String sql,Class<T> clazz,List<Object> params)throws Exception{
		if(clazz == Integer.class || clazz == String.class || clazz == Long.class){
			return jdbcTemplate.queryForList(sql,clazz,params.toArray());
		}
		return  jdbcTemplate.query(sql,new BeanPropertyRowMapper<T>(clazz),params.toArray());
		
	}
	
	
	public <T> void queryListByPage(String countsql,List<Object> countParams, String listSql,List<Object> listParams,
			HzlqswPage<T> page,Class<T> clazz)throws Exception{
		
		int totalNum = this.queryForObject(countsql,Integer.class,countParams);
		if(totalNum > 0){
			// 查询分页信息
			int length = page.getPageSize();
			int offset = (page.getCurPage() - 1) * length;
			listSql = SQLUtil.getPageSQL(listSql, offset, length, DEFAULT_SQL_TYPE);
			List<T> list = this.queryForList(listSql, clazz, listParams);
			// 设置分页信息（集合和记录总数）
			page.setPageList(list);
			page.setTotalNum(totalNum);
		}

	}
	
	
	public <T> void queryListByPage(String countsql,List<Object> countParams, 
			String listSql,List<Object> listParams,HzlqswPage<T> page,Class<T> clazz,String sql_type)throws Exception{
		
		int totalNum = this.queryForObject(countsql,Integer.class,countParams);
		if(totalNum > 0){
			// 查询分页信息
			int length = page.getPageSize();
			int offset = (page.getCurPage() - 1) * length;
			listSql = SQLUtil.getPageSQL(listSql, offset, length, sql_type);
			List<T> list = this.queryForList(listSql, clazz, listParams);
			// 设置分页信息（集合和记录总数）
			page.setPageList(list);
			page.setTotalNum(totalNum);
		}

	}
	
	
	public int updateObject(String sql,Object ... args)throws Exception{
		return this.jdbcTemplate.update(sql, args);
	}
	
	public int updateObject(String sql,List<Object> params)throws Exception{
		return this.jdbcTemplate.update(sql, params.toArray());
	}



}
